package com.simple.util.object;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Validation information bean. This bean used to store one item of validated information.
 * 
 * <pre>
 * Pattern : 
 * Thread Safe : No
 * 
 * Change History
 * 
 * Name              Date                 Description
 * --------          ------------         ----------------
 * Johnson Yang 	2 , 5 , 2010         Create the class
 * 
 * 
 * </pre>
 * 
 * @author kenst.xu
 * @version 1.0
 * 
 * @see 
 */
public class PageInfoBean  implements Serializable{
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = -6114756075714827410L;
    public final static int COMMON_RECORD_COUNT_IN_ONE_PAGE = 8;
    public final static int COMMON_PAGE_COUNT_IN_ONE_TIME = 10;
    
	private int curPageNo;
	private int totalPageNo;
	private int recordCountInOnePage;
	private int totalRecordCount = 0;
	
	public PageInfoBean(final int curPageNo){
	    this.curPageNo = curPageNo;
	    this.recordCountInOnePage=COMMON_RECORD_COUNT_IN_ONE_PAGE;
	}
	
	public PageInfoBean(final int curPageNo, final int recordCountInOnePage){
        this.curPageNo = curPageNo;
        this.recordCountInOnePage=recordCountInOnePage;
    }

    public int getCurPageNo() {
        return curPageNo;
    }

    public void setCurPageNo(final int curPageNo) {
        this.curPageNo = curPageNo;
    }

    public int getTotalPageNo() {
        return totalPageNo;
    }

    public void setTotalPageNo(final int totalPageNo) {
        this.totalPageNo = totalPageNo;
    }

    public void setRecordCountInOnePage(final int recordCountInOnePage) {
        this.recordCountInOnePage = recordCountInOnePage;
    }

    public int getRecordCountInOnePage() {
        return recordCountInOnePage;
    }

    public void setTotalRecordCount(final int totalRecordCount) {
        this.totalRecordCount = totalRecordCount;
    }

    public int getTotalRecordCount() {
        return totalRecordCount;
    }

    public void doReviseData(){
        if(curPageNo<=0){
            curPageNo = 1;
        }
        if(recordCountInOnePage<=0){
            recordCountInOnePage = COMMON_RECORD_COUNT_IN_ONE_PAGE;
        }
        
        if(totalRecordCount<0){
            totalRecordCount = 0;
        }

        totalPageNo = (int) Math.ceil((double)totalRecordCount/(double)recordCountInOnePage);
        if(totalPageNo == 0){
            totalPageNo = 1;
        }
        
        if(curPageNo > totalPageNo){
            curPageNo = totalPageNo;
        }
    }
    
    /**
     * extract records in current page from allRecordList. Please note that the page split rule and current page has already been set in this PageInfoBean.
     * @param <T>
     * @param allRecordList
     * @return
     * @author 020004
     */
    public <T> List<T> extractRecords(final List<T> allRecordList) {
    	final List<T> result = new ArrayList<T>();
    	this.setTotalRecordCount(allRecordList.size());
    	this.doReviseData();
    	final int beginNo = (getCurPageNo()-1) * getRecordCountInOnePage();
    	for(int i=0;i<getRecordCountInOnePage();i++){
            if(beginNo+i >= getTotalRecordCount()){
                break;
            }
            result.add(allRecordList.get(beginNo+i));
    	}
    	return result;
    }
    
    /**
     * extract records in currecnt page, max record count is maxRecordCount
     * @param <T>
     * @param allRecordList
     * @param maxRecordCount
     * @return
     */
    public <T> List<T> extractRecords(final List<T> allRecordList, final int maxRecordCount) {
    	final List<T> result = new ArrayList<T>();
        this.setTotalRecordCount(allRecordList.size()>maxRecordCount?maxRecordCount:allRecordList.size());
        this.doReviseData();
        final int beginNo = (getCurPageNo()-1) * getRecordCountInOnePage();
        for(int i=0;i<getRecordCountInOnePage();i++){
            if(beginNo+i >= getTotalRecordCount()){
                break;
            }
            result.add(allRecordList.get(beginNo+i));
        }
        return result;
    }
}
